import 'package:flutter/material.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class TestPage extends StatefulWidget {
  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  IOWebSocketChannel channel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    createSocket();
  }

  createSocket() {
    print('开始连接');
    channel = new IOWebSocketChannel.connect(
        "ws://192.168.43.21:8080?userID=6097d8d4c1102a36903b5871");
    channel.stream.listen((event) {
      print(event);
    }, onError: (err) {
      print(err);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    channel.sink.close();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('test'),
      ),
    );
  }
}
